<?php

namespace app\admin\service;

use app\model\Applet;
use think\facade\Cache;

class AppletService
{
    private $appletModel;

    public function __construct(Applet $appletModel)
    {
        $this->appletModel = $appletModel;
    }

    // 添加小程序
    public function editApplet($params)
    {
        if (empty($params['id'])) {
            $params['created_at'] = time();
        } else {
            $params['updated_at'] = time();
        }

        if (!empty($params['is_open_ad']) && empty($params['ad_id'])) {
            return  false;
        }

        $scenes = array_keys(Applet::AD_SCENE_LIST);
        if (!empty($params['ad_id'])) {
            $isVideoScene = false;
            foreach ($params['ad_id'] as $param) {
                if ($param['key'] == Applet::AD_SCENE_1) {
                    $isVideoScene = true;
                }
                if (!in_array($param['key'], $scenes)) {
                    return false;
                }
            }
            if (!$isVideoScene) {
                return false;
            }
            $params['ad_id'] = json_encode($params['ad_id'], JSON_UNESCAPED_UNICODE);
        }

        $id = $this->appletModel->edit($params);

        if (!$id) {
            return false;
        }
        $redis = Cache::store('redis')->handler();
        $redis->select(5);
        $appletKey = sprintf('redis_key.api.get_applet_config_%s', $params['app_id']);
        $redis->del($appletKey);
        return true;
    }

    // 获取小程序详情
    public function getAppletDetail($id)
    {
        $data = $this->appletModel->getInfoById($id, 'id, name, app_id, app_secret, package, status, type, token, salt, url, ios_pay_switch, share_switch, origin_id, template_id, share_img_url, activity_pay_show, company_entity_id, virtual_pay_switch, payment_rate, customer_service_id,is_open_ad,ad_id,vt_id');
        $data['ad_id'] = $data['ad_id'] ? json_decode($data['ad_id'], true) : [];
        return $data;
    }

    // 获取小程序列表
    public function getAppletList($params)
    {
        $page = $params['page'] ?? 1;
        $limit = $params['limit'] ?? 10;

        $where = [];
        if (!empty($params['type'])) {
            $where[] = ['type', '=', $params['type']];
        }

        if (!empty($params['name'])) {
            $where[] = ['name', 'like', '%' . $params['name'] . '%'];
        }
        $data = $this->appletModel->getPagingDataByWhere($where, $page, $limit, 'id, name, app_id, app_secret, origin_id, status, package, type, ios_pay_switch, share_switch, activity_pay_show,company_entity_id,virtual_pay_switch');
        return $data;
    }

    public function getAdSceneList(): array
    {
        $data = Applet::AD_SCENE_LIST;
        $newData = [];
        foreach ($data as $key => $item) {
            $newData[] = [
                'key' => $key,
                'value' => $item
            ];
        }
        return $newData;
    }

    // 获取下拉列表
    public function getAppletSelectList($params)
    {
        if (!empty($params['type'])) {
            $where[] = ['type', '=', $params['type']];
        }
        $where[] = ['status', '=', 1];
        $data = $this->appletModel->getDataByWhere($where);
        return $data;
    }

    // 删除小程序
    public function deleteApplet($id)
    {
        $bool = $this->appletModel->destroy($id);

        if ($bool == false) return false;

        return true;
    }
}
